<template>
  <div class="column-detail-page w-75 mx-auto">
    <div class="column-info row mb-4 border-bottom pb-4 align-items-center" v-if="columnScale">
      <div class="col-3 text-center">
        <img :src="columnScale.avatar && columnScale.avatar.url" :alt="columnScale.title" class="rounded-circle border">
      </div>
      <div class="col-9">
        <h4>{{columnScale.title}}</h4>
        <p class="text-muted">{{columnScale.description}}</p>
      </div>
    </div>
    <post-list :currentId="currentId" :list="list"></post-list>
  </div>
</template>

<script lang="ts">
/* 使用store异步加载数据 */
import { defineComponent, onMounted,computed } from 'vue'
import { useRoute } from 'vue-router'

import {useStore} from 'vuex'
import {GlobalDataProps,ColumnProps} from '@/store/index'
import PostList from '@/components/PostList/PostList.vue'

export default defineComponent({
  components: {
    PostList
  },
  setup() {
    const route = useRoute()
    const store = useStore<GlobalDataProps>()
    const currentId = route.query.id
    onMounted(() => {
    // store.dispatch('fetchDetailColumn',{id:currentId})
    // store.dispatch('fetchPosts',{id:currentId})


    })

    

    
    // const column = computed(()=> store.state.columns.find(c => String(c.id) == currentId))

    // const list = computed(()=> store.state.posts.filter(item => {
    //   return String(item.columnId) == currentId
    // }))

    // 采用 store 的getters方式
    // const column  = store.getters.getColumnById(currentId) as ColumnProps
    // console.log('详情：',column);

    const columnScale = computed(() => {
      // 获取的column数据必须是在计算属性里面，不能放在计算属性外，否则提示错误
        const column  = store.getters.getColumnById(currentId) as ColumnProps
        if (column.avatar) {
          const imgstr = '?x-oss-process=image/resize,m_pad,h_50,w_50,color_FF0000'
          const imgUrl = column.avatar.url || ''
          const reg = RegExp(/x-oss-process=image/)
          column.avatar.url = reg.test(imgUrl)?imgUrl:(imgUrl + imgstr)

        }
        return column
    }) 

    
    
    // const list  = store.getters.getPostListById(currentId) // 这里是原来的获取数据的方式
    const list = computed(()=> store.state.posts) //这是后来获取数据的方式
  
    return {
      // column,
      list,
      currentId: Number(currentId),
      columnScale
    }
  }
})
</script>
<style scoped>
  .text-center img {
    width: 100px;
    height: 100px;
  }
</style>
